﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace AdminApp.ViewModels
{
    public class HomeViewModel : ViewModelBase
    {
        public HomeViewModel()
        {
            WebContext.Current.Authentication.LoggedIn += (s, e) => UpdateForUserRole();
            WebContext.Current.Authentication.LoggedOut += (s, e) => UpdateForUserRole();

            //_registerCommand = new RelayCommand(OnRegister);
            //_unregisterCommand = new RelayCommand(OnUnregister);

            UpdateForUserRole();
        }

        private HashSet<int> _currentUserRegisteredEventIds;

        //private readonly RelayCommand _registerCommand;
        //public ICommand RegisterCommand
        //{
        //    get { return _registerCommand; }
        //}

        //private readonly RelayCommand _unregisterCommand;
        //public ICommand UnregisterCommand
        //{
        //    get { return _unregisterCommand; }
        //}
        
        private Visibility _adminButtonsVisibility;
        public Visibility AdminButtonsVisibility
        {
            get { return _adminButtonsVisibility; }
            set
            {
                if (_adminButtonsVisibility != value)
                {
                    _adminButtonsVisibility = value;
                    OnPropertyChanged("AdminButtonsVisibility");
                }
            }
        }
        private Visibility _attendeeButtonsVisibility;
        public Visibility AttendeeButtonsVisibility
        {
            get { return _attendeeButtonsVisibility; }
            set
            {
                if (_attendeeButtonsVisibility != value)
                {
                    _attendeeButtonsVisibility = value;
                    OnPropertyChanged("AttendeeButtonsVisibility");
                }
            }
        }
        private void UpdateForUserRole()
        {
            bool isLoggedIn = WebContext.Current.User.IsAuthenticated;
            bool isAdmin = isLoggedIn &&
                           WebContext.Current.User.IsInRole("Admins");

             AdminButtonsVisibility = isAdmin ? Visibility.Visible : Visibility.Collapsed;
             AttendeeButtonsVisibility = (isLoggedIn && !isAdmin) ? Visibility.Visible : Visibility.Collapsed;


             //if (isLoggedIn)
             //{
             //    var ctx = new EventManagerDomainContext();
             //    ctx.FetchEventsForWhichCurrentUserIsRegistered((op) =>
             //    {
             //        if (!op.HasError)
             //        {
             //            var items = op.Value;
             //            _currentUserRegisteredEventIds = new HashSet<int>(items);
             //            UpdateRegistrationButtons();
             //        }
             //    }, null);
             //}
             //else
             //{
             //    _currentUserRegisteredEventIds = null;
             //    UpdateRegistrationButtons();
             //}

        }

        //private void OnRegister()
        //{
        //    if (SelectedEvent != null)
        //    {
        //        var ctx = new EventManagerDomainContext();
        //        ctx.RegisterCurrentUserForEvent(SelectedEvent.EventID, (op) =>
        //        {
        //            UpdateForUserRole();
        //        }, null);
        //    }
        //}
        //private void OnUnregister()
        //{
        //    if (SelectedEvent != null)
        //    {
        //        var ctx = new EventManagerDomainContext();
        //        ctx.UnregisterCurrentUserForEvent(SelectedEvent.EventID, (op) =>
        //        {
        //            UpdateForUserRole();
        //        }, null);
        //    }
        //}


        //private Event _selectedEvent;
        //public Event SelectedEvent
        //{
        //    get { return _selectedEvent; }
        //    set
        //    {
        //        _selectedEvent = value;
        //        UpdateRegistrationButtons();
        //    }
        //}

        //private void UpdateRegistrationButtons()
        //{
        //    _registerCommand.IsEnabled = _currentUserRegisteredEventIds != null &&
        //         SelectedEvent != null &&
        //        !_currentUserRegisteredEventIds.Contains(SelectedEvent.EventID);

        //    _unregisterCommand.IsEnabled = _currentUserRegisteredEventIds != null &&
        //         SelectedEvent != null &&
        //        _currentUserRegisteredEventIds.Contains(SelectedEvent.EventID);
        //}
    }
}
